﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using QLVMBAYDatacess;
using QLVMBAYDomain;

namespace QLVMBAY.Admin
{
    public partial class QLGiaVe : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }
        private void LoadGrid()
        {
            grdGiaVe.DataSource = ChiTietCBayDA.Inst.GetAll(true);
            grdGiaVe.DataBind();
            if (grdGiaVe.PageCount <= 1)
            {
                grdGiaVe.PagerStyle.Visible = false;
            }
        }
        void Load_dlChuyenBay()
        {
            List<ChuyenBay> list = new List<ChuyenBay>();
            list = ChuyenBayDA.Inst.GetAll(true);
            dlChuyenBay.DataSource = list;
            dlChuyenBay.DataTextField = "TenChuyenBay";
            dlChuyenBay.DataValueField = "MaChuyenBay";
            dlChuyenBay.DataBind();
            dlChuyenBay.Items.Insert(0, new ListItem("Chọn", "0"));
        }
        
        void Load_dlLoaiGV()
        {
            List<Ve_Ghe > list = new List<Ve_Ghe>();

            list = VeGheDA.Inst.GetAll(true);
            var lstAll = list.Select(r => new { MaGheVe = r.MaGheVe, TenGheVe = r.ItemLoaiGhe.TenLoaiGhe + " - " + r.ItemLoaiVe.TenLoaiVe }).ToList();
            dlLoaiGV.DataSource = lstAll;
            dlLoaiGV.DataTextField = "TenGheVe";
            dlLoaiGV.DataValueField = "MaGheVe";
            dlLoaiGV.DataBind();
            dlLoaiGV.Items.Insert(0, new ListItem("Chọn", "0"));
        }
        private void ResetControlValues()
        {
            txtGiaVe.Text = "0";
            txtSLConLai.Text = "0";
            txtSLGhe.Text = "0";            
        }
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            pnView.Visible = false;
            Insert = true;
            txtMa.Visible = false;
            lblMa.Visible = false;
            ResetControlValues();
            Load_dlChuyenBay();
            Load_dlLoaiGV();
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
           
            if (Page.IsValid)
            {
                ChiTietChuyenBay CTCB = new ChiTietChuyenBay();                                
                CTCB.ItemChuyenBay = new ChuyenBay() { MaChuyenBay = dlChuyenBay.SelectedValue };
                CTCB.ItemCTGheVe = new Ve_Ghe() { MaGheVe = int.Parse(dlLoaiGV.SelectedValue) };                
                CTCB.GiaVe = decimal.Parse(txtGiaVe.Text);
                CTCB.SLGhe = int.Parse(txtSLGhe.Text);
                CTCB.SLGheConLai = int.Parse(txtSLConLai.Text);
                
                if (Insert == true)
                {
                    ChiTietCBayDA.Inst.Insert(CTCB);
                }
                else
                {
                    CTCB.MaCTCBay = int.Parse(txtMa.Text);
                    ChiTietCBayDA.Inst.Update(CTCB);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdGiaVe_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemCCB = ChiTietCBayDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;                    
                    Load_dlChuyenBay();
                    dlChuyenBay.SelectedValue = itemCCB.ItemChuyenBay.MaChuyenBay;                    
                    Load_dlLoaiGV();
                    dlLoaiGV.SelectedValue = itemCCB.ItemCTGheVe.MaGheVe.ToString();
                    txtGiaVe.Text = String.Format("{0:0,0}", itemCCB.GiaVe);
                    txtSLGhe.Text = itemCCB.SLGhe.ToString();
                    txtSLConLai.Text = itemCCB.SLGheConLai.ToString();
                    txtMa.Text = itemCCB.MaCTCBay.ToString();
                    txtMa.Visible = true ;
                    txtMa.ReadOnly = true;
                    lblMa.Visible = true;
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    ChiTietCBayDA.Inst.Delete(itemCCB);
                    LoadGrid();
                    break;
            }
        }

        protected void grdGiaVe_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                var CCB = (ChiTietChuyenBay)e.Item.DataItem;
                var lbl1 = e.Item.FindControl("lblTenLG") as Label;
                lbl1.Text = CCB.ItemCTGheVe.ItemLoaiGhe.TenLoaiGhe;                    
                var lbl2 = e.Item.FindControl("lblTenLV") as Label;
                lbl2.Text = CCB.ItemCTGheVe.ItemLoaiVe.TenLoaiVe;
                var lbl3 = e.Item.FindControl("lblTenCB") as Label;
                lbl3.Text = CCB.ItemChuyenBay.TenChuyenBay;
                var lbl4 = e.Item.FindControl("lblGiaVe") as Label;
                lbl4.Text = String.Format("{0:0,0}", CCB.GiaVe);
                    
            }
        }

        protected void grdGiaVe_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdGiaVe.CurrentPageIndex = e.NewPageIndex;
            LoadGrid();
        }
    }
}